home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / VirtualFile / VirtualMacFile.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  5.9 KB  |  279 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        VirtualMacFile.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef __ERRORS__
  15. #include <Errors.h>
  16. #endif
  17.  
  18. #ifndef __VIRTUALMACFILE__
  19. #include "VirtualMacFile.h"
  20. #endif
  21.  
  22. #ifndef    __VIRTUALFILE__
  23. #include "VirtualFile.h"
  24. #endif
  25.  
  26. #ifndef __VIRTUALHFSMACFILE__
  27. #include "VirtualHFSMacFile.h"
  28. #endif
  29.  
  30. #ifndef    __DEBUGGINGGEAR__
  31. #include "DebuggingGear.h"
  32. #endif
  33.  
  34. #ifndef __VVFILEMACFILE__
  35. #include "VVFileMacFile.h"
  36. #endif
  37.  
  38. /***********************************|****************************************/
  39.  
  40. #pragma segment VirtualMacFile
  41.  
  42. /***********************************|****************************************/
  43.  
  44. ostream& TVirtualMacFile::operator >> ( ostream& s ) const
  45. {
  46.     return s << "TVirtualMacFile @ " << ( void*) this;
  47. }
  48.  
  49. //--------------------------------------------------------------------------------
  50.  
  51. TVirtualMacFile::TVirtualMacFile():
  52.     THandleObject ()
  53. {
  54. }
  55.  
  56. //--------------------------------------------------------------------------------
  57.  
  58. TVirtualMacFile::TVirtualMacFile( const TVirtualMacFile& that ):
  59.     THandleObject ( that )
  60. {
  61. }
  62.  
  63. //--------------------------------------------------------------------------------
  64.  
  65. TVirtualMacFile::~TVirtualMacFile()
  66. {
  67. }
  68.  
  69. /***********************************|****************************************/
  70.  
  71. TVirtualMacFile&
  72. TVirtualMacFile::operator = ( const TVirtualMacFile& that )
  73. {
  74.     THandleObject::operator = ( that );
  75.     return *this;
  76. }
  77.  
  78. /***********************************|****************************************/
  79.  
  80. OSErr
  81. TVirtualMacFile::WriteToDisk ()
  82. {
  83.     FSSpec spec;
  84.     GetSpec ( spec );
  85.     return WriteToDisk ( spec );
  86. }
  87.  
  88. //--------------------------------------------------------------------------------
  89.  
  90. OSErr TVirtualMacFile::WriteToDisk ( const FSSpec& storage )
  91. {
  92.     TVirtualMacFile* tempFile = new TVirtualHFSMacFile ( storage );
  93.  
  94.     if ( !tempFile )
  95.         return memFullErr;
  96.  
  97.     OSErr error = tempFile->Open ();
  98.  
  99.     if ( !error )
  100.         error = Copy ( tempFile );
  101.     
  102.     tempFile->Close();
  103.  
  104.     delete tempFile;
  105.  
  106.     return error;
  107. }
  108.  
  109. /***********************************|****************************************/
  110.  
  111. OSErr
  112. TVirtualMacFile::CopyFork ( TVirtualMacFile::ForkType fork, TVirtualMacFile* destination )
  113. {
  114.     const unsigned long kBufferSize = 1024;
  115.     long fileSize, origPosition;
  116.  
  117.     OSErr error = GetEnd ( fileSize, fork );
  118.  
  119.     if ( error )
  120.         return error;
  121.  
  122.     error = GetPosition ( origPosition, fork );
  123.  
  124.     if ( error )
  125.         return error;
  126.  
  127.     error = SetPosition ( fsFromStart, 0, fork );
  128.  
  129.     if ( error )
  130.         return error;
  131.  
  132.     error = destination->SetEnd ( 0, fork );
  133.  
  134.     if ( error )
  135.         return error;
  136.  
  137.     error = destination->SetPosition ( fsFromStart, 0, fork );
  138.  
  139.     if ( error )
  140.         return error;
  141.  
  142.     if ( fileSize > 0 )
  143.     {
  144.         Ptr    buffer = FAILNewPtr ( kBufferSize );
  145.  
  146.         TRY
  147.         {
  148.             do
  149.             {
  150.                 long chunkSize = fileSize > kBufferSize ? kBufferSize : fileSize;
  151.                 error = ReadData ( buffer, chunkSize, fork );
  152.  
  153.                 if ( !error )
  154.                     error = destination->WriteData ( buffer, chunkSize, fork );
  155.  
  156.                 fileSize -= chunkSize;
  157.             }
  158.             while ( !error && fileSize > 0 );
  159.         }
  160.         EXCEPTION
  161.         {
  162.         }
  163.         ENDEXCEPTION
  164.  
  165.         DeallocatePtr ( buffer );
  166.     }
  167.  
  168.     SetPosition ( fsFromStart, origPosition, fork );
  169.  
  170.     return error;
  171. }
  172.  
  173. /***********************************|****************************************/
  174.  
  175. OSErr
  176. TVirtualMacFile::Copy ( TVirtualMacFile* destination )
  177. {
  178.     OSErr error = CopyFork ( TVirtualMacFile::kData, destination );
  179.  
  180.     if ( error )
  181.         return error;
  182.  
  183.     error = CopyFork ( TVirtualMacFile::kResource, destination );
  184.  
  185.     if ( error )
  186.         return error;
  187.  
  188.     FInfo finderInfo;
  189.     error = GetFinderInfo ( finderInfo );
  190.  
  191.     if ( error )
  192.         return error;
  193.  
  194.     error = destination->SetFinderInfo ( finderInfo );
  195.  
  196.     if ( error )
  197.         return error;
  198.  
  199.     destination->SetUserRef ( GetUserRef () );
  200.  
  201.     return error;
  202. }
  203.  
  204. /***********************************|****************************************/
  205.  
  206. TVirtualMacFile*
  207. TVirtualMacFile::Clone ()
  208. {
  209.     Str31 fileName;
  210.     GetFileName ( fileName );
  211.     TVVFileMacFile*    destination = new TVVFileMacFile ( fileName );
  212.     destination->Open ();
  213.     Copy ( destination );
  214.     destination->Close ();
  215.     return destination;
  216. }
  217.  
  218. //--------------------------------------------------------------------------------
  219.  
  220. OSErr TVirtualMacFile::GetFileInfo ( FSSpec& spec, HParamBlockRec& paramBlock )
  221. {
  222.     paramBlock.fileParam.ioVRefNum = spec.vRefNum;
  223.     paramBlock.fileParam.ioDirID = spec.parID;
  224.     paramBlock.fileParam.ioFDirIndex = 0;
  225.     paramBlock.fileParam.ioNamePtr = spec.name;
  226.     return PBHGetFInfo ( ¶mBlock, false );
  227. }
  228.  
  229. //--------------------------------------------------------------------------------
  230.  
  231. OSErr TVirtualMacFile::SetFileInfo ( FSSpec& spec, HParamBlockRec& paramBlock )
  232. {
  233.     paramBlock.fileParam.ioVRefNum = spec.vRefNum;
  234.     paramBlock.fileParam.ioDirID = spec.parID;
  235.     paramBlock.fileParam.ioFDirIndex = 0;
  236.     paramBlock.fileParam.ioNamePtr = spec.name;
  237.     return PBHSetFInfo ( ¶mBlock, false );
  238. }
  239.  
  240. //--------------------------------------------------------------------------------
  241.  
  242. OSErr TVirtualMacFile::SetFileName(const Str31 name)
  243. {
  244.     FSSpec spec;
  245.     OSErr error = GetSpec ( spec );
  246.     PLstrcpy ( spec.name, name );
  247.     return SetSpec ( spec );
  248. }
  249.  
  250. //--------------------------------------------------------------------------------
  251.  
  252. OSErr TVirtualMacFile::GetFileName(Str31 name) const
  253. {
  254.     FSSpec spec;
  255.     OSErr error = GetSpec ( spec );
  256.     PLstrcpy(name,spec.name);
  257.     return error;
  258. }
  259.  
  260. //--------------------------------------------------------------------------------
  261.  
  262. OSErr ChangeFileDates(const FSSpec& orig, long modDate, long creationDate)
  263. {
  264.     FSSpec spec = orig;
  265.     HParamBlockRec paramBlock;
  266.     OSErr err = TVirtualMacFile::GetFileInfo ( spec, paramBlock );
  267.  
  268.     if ( err )
  269.         return err;
  270.  
  271.     paramBlock.fileParam.ioDirID = spec.parID;
  272.     paramBlock.fileParam.ioFlCrDat = creationDate;
  273.     paramBlock.fileParam.ioFlMdDat = modDate;
  274.  
  275.     return TVirtualMacFile::SetFileInfo ( spec, paramBlock );
  276. }
  277.  
  278. /***********************************|****************************************/
  279.